package com.bpz.jxw.article.service.impl;

import com.bpz.jxw.article.dao.ArticleDao;
import com.bpz.jxw.article.model.Article;
import com.bpz.jxw.article.service.IArticleService;
import com.bpz.main.model.ActMsg;
import com.bpz.main.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 半瓶子 on 2/7/15.
 * blog: xusir.cnblogs.com
 */
@Service
public class ArticleService implements IArticleService {
    @Autowired
    ArticleDao articleDao;

    /**
     * 分页查询
     *
     * @param interval  某小时之内的数据
     * @param type      数据的类型 -1取所有，0，纯文，1，图文，2视频
     * @param pageIndex 页码
     * @param pageSize  页记录
     * @return
     */
    public ActMsg<Article> selectPage(int interval, int type, int pageIndex, int pageSize,String orderField) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("seconds", interval * 60 * 60);
        map.put("status", 1);
        if (type > -1) {
            map.put("type", type);
        }
        map.put("time", new Date().getTime() / 1000);
        map.put("order",orderField);
        Page page = new Page(pageIndex, pageSize);
        page.setCondition(map);
        List<Article> articles = articleDao.selectPage(page);
        ActMsg<Article> actMsg = new ActMsg<Article>();
        if (articles == null || articles.size() == 0) {
            actMsg.setCode(ActMsg.ERROR);
            actMsg.setMsg("未加载到数据");
        } else {
            actMsg.setCode(ActMsg.SUCCESS);
            actMsg.setMsg("数据加载成功");
            actMsg.setRows(articles);
        }
        return actMsg;
    }

    /**
     * 统计总数
     * @param interval
     * @param type
     * @return
     */
    public int count(int interval, int type) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("seconds", interval * 60 * 60);
        map.put("status", 1);
        if (type > -1) {
            map.put("type", type);
        }
        map.put("time", new Date().getTime() / 1000);
        int total = articleDao.countMap(map);
        return total;
    }

    /**
     * 分页查询
     *
     * @param map       查询条件
     * @param pageIndex 当前页索引
     * @param pageSize  每页记录数
     * @return
     */
    @Override
    public List<Article> selectPage(HashMap<String, Object> map, int pageIndex, int pageSize) {
        return null;
    }

    /**
     * 投票
     *
     * @param id
     * @param act 投票方式1点赞 -1拍砖
     * @return
     */
    @Override
    public ActMsg voteTo(int id, int act) {
        ActMsg msg=new ActMsg();
        if(id>0&&(act==-1||act==1))
        {
            Map<String,Integer> map=new HashMap<String, Integer>();
            map.put("id",id);
            map.put("act",act);
            articleDao.voteTo(map);
            msg.setCode(ActMsg.SUCCESS);
            msg.setMsg("更新成功");
        }else
        {
            msg.setCode(ActMsg.ERROR);
            msg.setMsg("更新失败，参数无效");
        }
        return msg;
    }

    /**
     * 获取三条记录，当前条和上下一条
     *
     * @param id
     * @return
     */
    @Override
    public List<Article> getNextAndPrev(int id) {

       List<Article> articles= articleDao.getNextAndPrev(id);
return articles;
    }
}
